package a3.permut

import scala.annotation.tailrec

class Permutation(val permut: Int*) {

  override def equals(in: Any): Boolean = { // ==
    in match {
      case k: Permutation if (k.permut == permut) => true
      case _ => false
    }
  }

  def fixPoints() = {
    @tailrec
    def fix(i: Int, count: Int*): List[Int] = {
      if (i < 5) {
        if (i + 1 == permut(i)) {
          fix(i + 1, (count :+ (i + 1)): _*)
        } else
          fix(i + 1, count: _*)
      } else
        count.toList
    }

    if (permut.length == 5) {
      fix(0)
    }
  }

  def cycles() = {
    @tailrec
    def _cycles(i: Int, search: Int, hops: Int*): List[Int] = {
      if (permut(i) != search)
        _cycles(i + 1, search, (hops :+ permut(i)): _*)
      else
        (hops :+ permut(i)).toList        
    }
    

    
    _cycles(0, 1)
  }
}

object Permutation {
  def apply(e: Int*): Permutation = {
    if (e.length != 5) {
    } else {
      val list = List[Int](e(0), e(1), e(2), e(3), e(4))
      for (i <- List(1, 2, 3, 4, 5).permutations) {
        if (i == list) {
          return new Permutation(e: _*)
        }
      }
    }
    new Permutation()
  }
}